﻿using System.ServiceModel;
using System.ServiceModel.Web;
using Interscape.TwilioClient.Components;
using Interscape.TwilioClient.ServiceHelpers;
using Microsoft.Http;

namespace Interscape.TwilioClient
{

    #region Service Interface

    [ServiceContract]
    public interface IAccountManager
    {

        [OperationContract]
        [TwilioWcfSerializer]
        [WebGet(
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml,
            UriTemplate = "/Accounts/{accountSid}"
            )]
        Account Get(string accountSid);

    }

    #endregion

    /// <summary>
    /// 
    /// </summary>
    public class AccountManager : ManagerBase<IAccountManager>, IAccountManager
    {

        #region Constructors

        /// <summary>
        /// Creates a new instance of the AccountManager.
        /// </summary>
        /// <param name="accountSid">The 34 character string that uniquely identifies the Account which will be used to make the TwilioAPI requests.</param>
        /// <param name="authToken">The "password" for your Twilio API calls.</param>
        public AccountManager(string accountSid, string authToken) : base(accountSid, authToken)
        {
        }

        #endregion

        /// <summary>
        /// Returns a representation of your Account
        /// </summary>
        /// <returns></returns>
        public Account Get()
        {
            return this.Get(base.AccountSid);
        }

        /// <summary>
        /// Returns a representation of your Account
        /// </summary>
        /// <param name="accountSid">A 34 character string that uniquely identifies the Account associated with this resource.</param>
        /// <returns></returns>
        public Account Get(string accountSid)
        {
            return this.Channel.Get(accountSid);
        }

        /// <summary>
        /// Updates an Account
        /// </summary>
        /// <param name="account">The Account to be updated</param>
        /// <returns>The updated Account object</returns>
        public Account Update(Account account)
        {
            System.Net.ServicePointManager.Expect100Continue = false;
            HttpUrlEncodedForm form = new HttpUrlEncodedForm();
            HttpResponseMessage response = null;
            
            //Add the required variables to the POST
            form.Add("FriendlyName", account.FriendlyName);

            response = this.HttpClientHelper.Post(this.Urls.AccountUrl(true), form.CreateHttpContent());
            return (Account)ServiceUtility.ProcessHttpResponseMessage(response, typeof(Account));
            
        }


    }
}
